package org.rescue.web;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;

import org.rescue.search.*;
import org.rescue.data.extraction.*;
import org.rescue.data.extraction.gate.ExtractGATE;



public class Search extends HttpServlet {

	public void doPost(HttpServletRequest request, HttpServletResponse response)
		throws IOException, ServletException {

		HttpSession session = request.getSession();
		
		//response.setContentType("text/html");
		String q = request.getParameter("QueryTextBox");
		int max = Integer.valueOf(request.getParameter("MaxResultsTextBox")).intValue();
	
		try {		
			Dataset d = new Dataset();
			d.setSearchBehavior(new SearchGoogle());
			d.setExtractBehavior(new ExtractGATE());
			d.setClassifyBehavior(new ClassifySimpleLocation());
			d.setQuery(q);
		
			d.performSearch(max);
			d.performExtraction();
			d.performClassification();
			
			session.setAttribute("query",q);
			session.setAttribute("maxResults", max);
			session.setAttribute("dataset", d);
			session.setAttribute("locations", d.getAllLocationsAsString());
		}
		catch (SearchException ex) {
			System.out.println(ex.getMessage());
		}
		
		RequestDispatcher view = request.getRequestDispatcher("search.jsp");
		view.forward(request,response);
		
		
	}
	
}
